package com.github.caijh.mall.common.validator;

import javax.inject.Inject;

import com.github.caijh.framework.data.redis.Redis;
import com.github.caijh.mall.constants.Constants;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;

@Component
public class FrontTokenValidator implements TokenRedisValidator {

    @Inject
    private Redis redis;

    //路由在此处，则返回true，无论用户是否登录都可以访问
    public boolean checkRouter(String uri) {
        String[] routerList = {
                "/api/front/product/detail",
                "/api/front/coupons",
                "/api/front/index",
                "/api/front/bargain/list",
                "/api/front/combination/list"
        };

        return ArrayUtils.contains(routerList, uri);
    }

    @Override
    public String getKey(String token) {
        return Constants.USER_TOKEN_REDIS_KEY_PREFIX + token;
    }

    @Override
    public String getValue(String key) {
        return redis.get(key);
    }

    @Override
    public void setValue(String key, String value) {
        redis.set(key, value, Constants.TOKEN_EXPIRE_SECONDS);
    }

}
